/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.qb9.flashlib.logs
{
	import com.qb9.flashlib.interfaces.IDisposable;
	import com.qb9.flashlib.utils.DisplayUtil;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * Simple text field useful for message logs.
	 *
	 * <p>Use this class when the global trace() function is not useful (eg, when the swf is running inside
	 * a browser and traces are not shown anywhere). (Alternatively, you can use BrowserConsoleAppender).</p>
	 *
	 * <p>Click empties the text field.</p>
	 *
	 * @example Usage example:
	 * <listing version="3.0">
	 * class Main extends Sprite
	 * {
	 *  private var log:LoggerSprite;
	 *
	 * 	public function Main()
	 * 	{
	 *   var log:Logger = new Logger();
	 *   var appender:TextFieldAppender = new TextFieldAppender();
	 *   this.addChild(appender);
	 *   log.addAppender(appender);
	 *   log.debug("Hello.");
	 * 	}
	 * }
	 * </listing> 
	 */
	public class TextFieldAppender extends Sprite implements IAppender, IDisposable
	{
		/**
		 * Text field used to display traced text.
		 */
		public var textField:TextField;

		public function TextFieldAppender(stayOnTop:Boolean = true)
		{
			this.textField = new TextField();
			this.textField.border = true;
			this.textField.background = true;
			this.textField.backgroundColor = 0xffffff;
			this.textField.defaultTextFormat = new TextFormat('_sans', 9, 0);
			this.textField.multiline = true;
			this.textField.wordWrap = true;
			this.addChild(textField);

			this.textField.addEventListener(MouseEvent.CLICK, this.onClick, true);
			this.addEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
			if (stayOnTop)
				this.addEventListener(Event.ENTER_FRAME, this.enterFrame);

			adjustSize();
		}

		protected function addedToStage(ev:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
			this.textField.width = stage.stageWidth;
		}

		protected function enterFrame(ev:Event):void
		{
			DisplayUtil.bringToFront(this);
		}

		/** @inheritDoc */
		public function append(msg:Array,level:int):void
		{
			this.textField.appendText(msg.join(" ") + "\n");
			adjustSize();
		}
		
		private function adjustSize():void
		{
			this.textField.height = Math.min(100, textField.textHeight);
			this.textField.scrollV = textField.numLines;
		}

		private function onClick(ev:MouseEvent):void
		{
			this.textField.text = '';
			adjustSize();
		}
		
		public function dispose():void
		{
			this.textField.removeEventListener(MouseEvent.CLICK, this.onClick, true);
			this.removeEventListener(Event.ADDED_TO_STAGE, this.addedToStage);
			this.removeEventListener(Event.ENTER_FRAME, this.enterFrame);
			this.textField = null;
		}
	}
}
